
public class p1009_failForLarge {

	public static//
	// -----
	// -----
	class Main {
		public static void main(String[] args) throws Exception {
			java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			while (true) {
				width = Integer.parseInt(reader.readLine());
				System.out.println(width);
				if (width == 0)
					break;
				init();
				while (true) {
					String[] parts = reader.readLine().split(" ");
					int num = Integer.parseInt(parts[0]);
					int count = Integer.parseInt(parts[1]);
					if (count == 0) {
						finish();
						break;
					}
					put(num, count);
				}
			}
		}

		static int width;
		static int[] prev, curr, next;
		static int nextSize, outVal, outCnt;

		static void init() {
			prev = null;
			curr = next = new int[width];
			nextSize = outCnt = 0;
		}

		/**
		 * +++++++++++++++++ ++++++++|1> (1) | | (2) | |00000(3)0000000|
		 * |000(0-zone)0000| |000000000000000| | (4) | | (next) |
		 */
		static void put(int num, int count) {
			if (count < width - nextSize) { // <1>
				java.util.Arrays.fill(next, nextSize, nextSize + count, num);
				nextSize += count;
				return;
			}
			if (nextSize > 0) { // (1)
				java.util.Arrays.fill(next, nextSize, width, num);
				count -= width - nextSize;
				nextSize = 0;
				calcAndMove();
			}

			if (count >= width * 3) { // has 0-zone
				java.util.Arrays.fill(next, 0, width, num); // (2)
				count -= width;
				calcAndMove();
				next = curr; // (3)
				count -= width;
				calcAndMove();

				int size = count - width - count % width;
				count -= size;
				print(0, size);

				if (count > 0)
					put(num, count);
			} else {
				if (count >= width) { // (2)
					count -= width;
					java.util.Arrays.fill(next, 0, width, num);
					calcAndMove();
					while (count >= width) { // (3~4)
						count -= width;
						next = curr;
						calcAndMove();
					}
				}
				if (count > 0)
					put(num, count);
			}
		}

		static void calcAndMove() {
			if (prev != null) {
				if (width > 1) {
					print(diff(curr[0], curr[1], prev[0], prev[1], next[0], next[1]), 1); // first
					for (int i = 1; i < width - 1; i++) {
						print(diff(curr[i], curr[i - 1], curr[i + 1], prev[i - 1], prev[i], prev[i + 1], next[i - 1],
								next[i], next[i + 1]), 1);
					}
					print(diff(curr[width - 1], curr[width - 2], prev[width - 1], prev[width - 2], next[width - 1],
							next[width - 2]), 1); // last
				} else {
					print(diff(curr[0], prev[0], next[0]), 1);
				}
			}
			prev = curr;
			curr = next;
			next = new int[width];
		}

		static void finish() {
			next = curr;
			calcAndMove();
			if (outCnt > 0)
				System.out.println(outVal + " " + outCnt);
			System.out.println("0 0");
		}

		static int diff(int cur, int... vals) {
			int max = 0;
			for (int v : vals)
				max = Math.max(max, Math.abs(cur - v));
			return max;
		}

		static void print(int val, int count) {
			if (outVal == val) {
				outCnt += count;
			} else {
				if (outCnt > 0) {
					System.out.println(outVal + " " + outCnt);
				}
				outVal = val;
				outCnt = count;
			}
		}
	}

	// -----
	// -----

	public static void main(String[] args) throws Exception {
		Judge.run();
	}

}
